//
//  TemporaryShowView.m
//  HomePage
//
//  Created by xiong qi on 13-5-20.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "TemporaryShowView.h"
#import "../../ResourceLoader/ResourceLoader/ResourceLoader.h"

@implementation TemporaryShowView
@synthesize autoarrays;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 240, 1024, 236)];
        [self addSubview:imageview];
        [imageview release];
        imageview.userInteractionEnabled = YES;
        
        UIButton * btncar1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 56, 90, 70)];
        [imageview addSubview:btncar1];
        [btncar1 release];
        btncar1.tag = 1;
        [btncar1 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
//        [btncar1 setBackgroundColor:[[UIColor yellowColor] colorWithAlphaComponent:0.5]];
        
        UIButton * btncar2 = [[UIButton alloc] initWithFrame:CGRectMake(95, 52, 100, 90)];
        [imageview addSubview:btncar2];
        [btncar2 release];
        btncar2.tag = 2;
        [btncar2 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
//        [btncar2 setBackgroundColor:[[UIColor blueColor] colorWithAlphaComponent:0.5]];
        
        UIButton * btncar3 = [[UIButton alloc] initWithFrame:CGRectMake(200, 40, 160, 120)];
        [imageview addSubview:btncar3];
        [btncar3 release];
        btncar3.tag = 3;
        [btncar3 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
//        [btncar3 setBackgroundColor:[[UIColor redColor] colorWithAlphaComponent:0.5]];
        
        UIButton * btncar4 = [[UIButton alloc] initWithFrame:CGRectMake(365, 25, 210, 180)];
        [imageview addSubview:btncar4];
        [btncar4 release];
        btncar4.tag = 4;
        [btncar4 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
//        [btncar4 setBackgroundColor:[[UIColor greenColor] colorWithAlphaComponent:0.5]];
        
        UIButton * btncar5 = [[UIButton alloc] initWithFrame:CGRectMake(585, 40, 180, 120)];
        [imageview addSubview:btncar5];
        [btncar5 release];
        btncar5.tag = 5;
        [btncar5 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
//        [btncar5 setBackgroundColor:[[UIColor purpleColor] colorWithAlphaComponent:0.5]];
        
        UIButton * btncar6 = [[UIButton alloc] initWithFrame:CGRectMake(775, 48, 100, 90)];
        [imageview addSubview:btncar6];
        [btncar6 release];
        btncar6.tag = 6;
        [btncar6 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
//        [btncar6 setBackgroundColor:[[UIColor grayColor] colorWithAlphaComponent:0.5]];
        
        UIButton * btncar7 = [[UIButton alloc] initWithFrame:CGRectMake(880, 56, 75, 70)];
        [imageview addSubview:btncar7];
        [btncar7 release];
        btncar7.tag = 7;
        [btncar7 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
//        [btncar7 setBackgroundColor:[[UIColor redColor] colorWithAlphaComponent:0.5]];
        
        UIButton * btncar8 = [[UIButton alloc] initWithFrame:CGRectMake(955, 56, 85, 70)];
        [imageview addSubview:btncar8];
        [btncar8 release];
        btncar8.tag = 8;
        [btncar8 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
//        [btncar8 setBackgroundColor:[[UIColor yellowColor] colorWithAlphaComponent:0.5]];
        
        [self RefreshSourceData];
    }
    return self;
}

-(void)RefreshSourceData
{
    ResourceLoader * loader = [ResourceLoader getInstance];
    [loader RefreshAutoDics];
    self.autoarrays = [loader getHomePageAutoData];
}

-(void)sleep
{
    imageview.image = nil;
}

-(void)awakeUp
{
    NSString * path = [NSString stringWithFormat:@"%@/AllAuto_Image_AutosBG@2x.png",[[NSBundle mainBundle] resourcePath]];
    UIImage * image = [[UIImage alloc] initWithContentsOfFile:path];
    if (image) {
        imageview.image = image;
        [image release];
    }
}

-(void)onClick:(UIButton *)sender
{
    
    NSDictionary * dic = nil;
    NSString * autoid = nil;
    switch (sender.tag) {
        case 1:
            autoid = @"EVERUSS1";
            break;
        case 2:
            autoid = @"FIT";
            break;
        case 3:
            autoid = @"ODYSSEY";
            break;
        case 4:
            autoid = @"CROSSTOUR";
            break;
        case 5:
            autoid = @"ACCORD";
            break;
        case 6:
            autoid = @"CRIDER";
            break;
        case 7:
            autoid = @"CITY";
            break;
        case 8:
            autoid = @"CR-Z";
            break;
            
        default:
            break;
    }
    
    NSDictionary * tempdic = nil;
    for (int i=0; i<[autoarrays count]; i++) {
        
        tempdic = [autoarrays objectAtIndex:i];
        if ([[tempdic objectForKey:@"autoid"] isEqual:autoid]) {
            dic = tempdic;
        }
    }
    
    if (dic) {
        [[NSNotificationCenter defaultCenter]postNotificationName:Notification_Navigate_SecondPage object:dic userInfo:nil];
    }
    
}

-(void)dealloc
{
    self.autoarrays = nil;
    [super dealloc];
}

@end
